/**
 * Created by shengjian.lin on 17-2-14.
 */

function business(path,successCode) {

    function get(url,param,methods) {
        $.post(path+url,param,function (result) {
            if(result.code == successCode){
                methods(result.data);
            }else{
                layer.msg(result.data);
            }
        },'json');
    }

    return {
        household:function () {
            return {
                dialog:function(){
                    var resultData;
                    function autocomplete(layero,el,request,response) {

                        var param = getHtmlMaster(layero);
                        if('code' == el){
                            param.code = request.term;
                        }else if('addr' == el){
                            param.collectAddress = request.term;
                        }else if('name' == el){
                            param.name = request.term;
                        }else if('idno' == el){
                            param.idNo = request.term;
                        }

                        $.post(
                            path+"/api/household/find",
                            param,
                            function (result) {
                                if(result.code == successCode){
                                    var array = [];
                                    $(result.data.content).each(function () {
                                        var value;
                                        if('code' == el){
                                            value = this.code;
                                        }else if('addr' == el){
                                            value = this.collectAddress;
                                        }else if('name' == el){
                                            value = this.name;
                                        }else if('idno' == el){
                                            value = this.idNo;
                                        }
                                        if (value != undefined && !$.isEmptyObject(value)){
                                            array.push(value)
                                        }
                                    });

                                    response(array);

                                    if(array.length == 1){
                                        select(layero,'id',result.data.content[0].id)
                                    }

                                }else{
                                    layer.msg(result.data);
                                }
                            },
                            'json'
                        );
                    }

                    function select(layero,el,value) {
                        var param = getHtmlMaster(layero);
                        if('id' == el){
                            param.id = value;
                        }else{
                            param[el] = value
                        }

                        $.post(
                            path+"/api/household/find",
                            param,
                            function (result) {

                                if(result.code == successCode){

                                    var data;
                                    if(result.data.content == undefined){
                                        data = result.data;
                                    }else{
                                        if(result.data.content.length ==1){
                                            data = result.data.content[0];
                                        }
                                    }
                                    if(data != undefined){
                                        $(layero).find('input[name="id"]').val(data.id);
                                        $(layero).find('input[name="code"]').val(data.code);
                                        $(layero).find('input[name="addr"]').val(data.collectAddress);
                                        $(layero).find('input[name="name"]').val(data.name);
                                        $(layero).find('input[name="idno"]').val(data.idNo);
                                    }
                                    resultData=data;
                                }else{
                                    layer.msg(result.data);
                                }
                            },
                            'json'
                        );
                    }

                    function getHtmlMaster(layero) {
                        var param = {};
                        param.id = $(layero).find('input[name="id"]').val();
                        param.id == undefined?'':param.id;

                        param.code = $(layero).find('input[name="code"]').val();
                        param.code == undefined?'':param.code;

                        param.collectAddress = $(layero).find('input[name="addr"]').val();
                        param.collectAddress == undefined?'':param.collectAddress;

                        param.name = $(layero).find('input[name="name"]').val();
                        param.name == undefined?'':param.name;

                        param.idNo = $(layero).find('input[name="idno"]').val();
                        param.idNo == undefined?'':param.idNo;

                        return param;
                    }

                    return {
                        html: function () {
                            var html = '\
                    <div class="ibox-content" style="overflow: hidden;height: 100%;">\
                        <form class="form-horizontal" method="post">\
                            <input type="text" name="id" style="display: none;">\
                            <div class="form-group">\
                                <label class="col-sm-2 control-label">档案号：</label>\
                                <div class="col-sm-10">\
                                    <input type="text" min=0 class="form-control" name="code">\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="col-sm-2 control-label">被征收人：</label>\
                                <div class="col-sm-10">\
                                    <input type="text" min=0 class="form-control" name="name">\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="col-sm-2 control-label">身份证号：</label>\
                                <div class="col-sm-10">\
                                    <input type="text" min=0 class="form-control" name="idno">\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="col-sm-2 control-label">土地坐落：</label>\
                                <div class="col-sm-10">\
                                    <input type="text" min=0 class="form-control" name="addr">\
                                </div>\
                            </div>\
                        </form>\
                    </div>';
                            return html;
                        }(),
                        init: function (layero) {
                            $(layero).find('input[name="code"]').autocomplete({
                                source: function (request, response) {
                                    autocomplete(layero, 'code', request, response);
                                },
                                select: function (event, ui) {
                                    select('code', ui.item.value);
                                }
                            });
                            $(layero).find('input[name="addr"]').autocomplete({
                                source: function (request, response) {
                                    autocomplete(layero, 'addr', request, response);
                                },
                                select: function (event, ui) {
                                    select('addr', ui.item.value);
                                }
                            });
                            $(layero).find('input[name="name"]').autocomplete({
                                source: function (request, response) {
                                    autocomplete(layero, 'name', request, response);
                                },
                                select: function (event, ui) {
                                    select('name', ui.item.value);
                                }
                            });
                            $(layero).find('input[name="idno"]').autocomplete({
                                source: function (request, response) {
                                    autocomplete(layero, 'idno', request, response);
                                },
                                select: function (event, ui) {
                                    select('idno', ui.item.value);
                                }
                            });
                        },
                        result:function () {
                            return resultData;
                        }
                    }
                },

            };
        },
        result:function (master,method) {
            var data = {master:master};
            //查询土地信息
            get('/api/identified/land/find',{masterId:master.id},function (result) {
                data.land = result.content;
                //查询房屋信息
                get('/api/identified/housing/find',{masterId:master.id},function (result) {
                    data.housing = result.content;
                    //查询搬迁过渡信息
                    get('/api/relocationTransition/get',{masterId:master.id},function (result) {
                        if(result == null)
                            result = {};
                        data.relocationTransition=result;
                        get('/api/reward/get',{masterId:master.id},function (result) {
                            if(result == null)
                                result = {};
                            data.onTime=result;
                            get('/api/identified/storefront/find',{masterId:master.id},function (result) {
                                data.storefront=result.content[0]==undefined?{}:result.content[0];
                                get('/api/housing/appendages/find',{masterId:master.id},function (result) {
                                    data.appendages=result.content;
                                    get('/api/identified/homestead/find',{masterId:master.id},function (result) {
                                        data.homestead=result.content[0]==undefined?{}:result.content[0];
                                        get('/api/housing/subsidies/find',{masterId:master.id},function (result) {
                                            data.subsidies=result.content;
                                            //数据全部准备完成
                                            method(data);
                                        });
                                    });
                                });

                            });
                        });
                    });
                });
            });
        },
        dispose:function (result) {
            return {
                overview:{
                    housing:function () {

                        var area = 0.0;
                        var layer = {number:0,area:0.0,structure:{}};
                        var compensate = 0.0;
                        $(result.housing).each(function () {
                            area += this.area;
                            if (layer.number == 0) {
                                layer.number = this.layer;
                                layer.area = this.area;
                                layer.structure = this.structureLevel;
                            }
                            if (layer.number != 1 && this.layer < layer.number) {
                                layer.number = this.layer;
                                layer.area = this.area;
                                layer.structure = this.structureLevel;
                            }
                            if(this.landStandard.pattern){
                                var _compensate = this.area*this.landStandard.ratio/100.0;
                                if(this.landStandard.formalities == 'without' && _compensate > 150){
                                    //无手续最大补偿面积不能大于150
                                    _compensate = 150;
                                }
                                if(this.layer > 0 && this.atticHighly == true){
                                    _compensate = 0;
                                }
                                if(this.layer < 0 && this.basement=='Third2MoreThanHalf'){
                                    _compensate*=0.5;
                                }
                                if(this.layer < 0 && this.basement=='LessThanThird'){
                                    _compensate*=0;
                                }
                                compensate += _compensate;
                            }
                        });

                        return {monolayer:layer.area,total:area,compensate:compensate,structure:layer.structure};
                    }(),
                    land:function () {
                        var building = {
                            compensate:0,
                            total:0
                        };
                        var clearing = 0;
                        $(result.land).each(function () {

                            if(this.standard.type == 'building'){
                                if(this.standard.pattern){
                                    var compensate = this.area*this.standard.ratio/100.0;
                                    if(this.standard.formalities == 'without' && compensate > 150){
                                        compensate=150;
                                    }
                                    building.compensate += compensate;
                                }
                                building.total += this.area;
                            }else{
                                clearing += this.area;
                            }
                        });
                        return {building:building,clearing:clearing};
                    }(),
                    resettlement:function (compensate) {
                        if($.isEmptyObject(result.relocationTransition))
                            return '';
                        if(compensate == result.relocationTransition.currencyCompensateArea){
                            return '全部货币补偿';
                        }else if(result.relocationTransition.currencyCompensateArea == 0){
                            return '全部产权调换';
                        }else{
                            return '部分货币补偿与部分产权调换相结合';
                        }
                    },
                    totalLayer:function (housing) {
                        var layer = 0;
                        $(housing).each(function () {
                            if((this.layer < 0 && this.basement=='MoreThanHalf') || (this.layer > 0 && this.attic != true && this.atticHighly != true))
                                layer++;
                        });
                        return layer;
                    },
                    maxBuyArea:function (compensate,layer,homestead) {
                        var area = 0.0;
                        if(layer >1 )
                            area += compensate * 0.5;
                        else
                            area += compensate;

                        if(homestead > 120)
                            area += 120;
                        else
                            area += homestead;

                        return area;
                    }
                },
                detail:{
                    housing:function () {
                        var structure = {};
                        var decorate = {};
                        $(result.housing).each(function () {

                            var structureKey = this.structureLevel.structure.name + this.structureLevel.name + this.structureIntoNew.name;
                            if(structure[structureKey] == undefined){
                                structure[structureKey] = {
                                    level:this.structureLevel.structure.name+this.structureLevel.name,
                                    intoNew:this.structureIntoNew.name,
                                    area:0
                                };
                            }
                            structure[structureKey].area += this.area;

                            var decorateKey = this.decorateLevel.name + this.decorateIntoNew.name;
                            if(decorate[decorateKey] == undefined){
                                decorate[decorateKey] = {
                                    level:this.decorateLevel.name,
                                    intoNew:this.decorateIntoNew.name,
                                    area:0
                                };
                            }
                            decorate[decorateKey].area += this.area;

                        });
                        return {structure:structure,decorate:decorate};
                    }(),
                },
                exchange:function (housing,area) {
                    var _housing = {};
                    var layer = 0;
                    $(housing).each(function () {
                        _housing[this.layer] = this;
                        if(this.layer > layer)
                            layer = this.layer;
                    });

                    var result = {housing:[],currency:[]};

                    for (var i=1;i<=layer;i++){
                        if(_housing[i] == undefined)
                            continue;
                        if(area != 0 && _housing[i].landStandard.pattern){

                            var compensate = _housing[i].area*_housing[i].landStandard.ratio/100.0;
                            if(_housing[i].landStandard.formalities == 'without' && compensate > 150){
                                compensate=150;
                            }
                            if(_housing[i].layer > 0 && _housing[i].atticHighly == true){
                                compensate = 0;
                            }
                            if(_housing[i].layer < 0 && _housing[i].basement=='Third2MoreThanHalf'){
                                compensate*=0.5;
                            }
                            if(_housing[i].layer < 0 && _housing[i].basement=='LessThanThird'){
                                compensate*=0;
                            }

                            var notCompensate = _housing[i].area - compensate;
                            var residue = compensate - area;
                            if(residue > 0){
                                var _e = {};
                                $.extend(_e,_housing[i]);
                                _e.compensate = compensate;
                                _e.notCompensate = 0;
                                area = 0;
                                result.housing.push(_e);
                                _housing[i].compensate = residue;
                                _housing[i].notCompensate = notCompensate;
                                result.currency.push(_housing[i]);
                            }else if(residue == 0){
                                var _e = {};
                                $.extend(_e,_housing[i]);
                                _e.compensate=area;
                                _e.notCompensate = notCompensate;
                                area = 0;
                                result.housing.push(_e);
                            }else{
                                area = Math.abs(residue);
                                _housing[i].compensate=compensate;
                                _housing[i].notCompensate = notCompensate;
                                result.housing.push(_housing[i]);
                            }

                        }else{
                            _housing[i].compensate = _housing[i].area;
                            _housing[i].notCompensate = 0;
                            result.currency.push(_housing[i]);
                        }
                    }

                    return result;
                }
            };
        },
        post:get
    }
}



